package com.qxzn.app.page.util.face;

import com.google.gson.Gson;
import com.qxzn.app.util.AppUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人脸
 */
public class FaceUtil {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */

    //向人脸库中添加人脸
    public static String add(String image_type, String image, String group_id, String user_id, String user_info) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        try {
            Map<String, Object> map = new HashMap<>();
//            map.put("image", "http://pic.qqtn.com/up/2019-2/2019022108024192583.jpg");//5ace6cfbe066635222f0ac5a29691d6f
            map.put("image", image);//8fd4859cba547f2c4f89700144322502
            map.put("image_type", image_type);
            map.put("group_id", group_id);
            map.put("user_id", user_id);
            map.put("user_info", user_info);
            map.put("liveness_control", "LOW");
            map.put("quality_control", "LOW");
            String param = GsonUtils.toJson(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
//          String accessToken = "[调用鉴权接口获取的token]";
            String result = HttpUtil.post(url, AuthService.getAuth(), "application/json", param);
            AppUtil.showLog(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            AppUtil.showLog("人脸注册异常：" + e);
        }
        return null;
    }

    //更新人脸
    public static String update(String image_type, String image, String group_id, String user_id, String user_info) {
        // 请求url
//        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/delete";
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);
            map.put("image_type", image_type);
            map.put("group_id", group_id);
            map.put("user_id", user_id);
            map.put("user_info", user_info);
            map.put("liveness_control", "LOW");
            map.put("quality_control", "LOW");
            String param = GsonUtils.toJson(map);
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
//          String accessToken = "[调用鉴权接口获取的token]";
            String result = HttpUtil.post(url, AuthService.getAuth(), "application/json", param);
            AppUtil.showLog(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            AppUtil.showLog("更新人脸异常：" + e);
        }
        return null;
    }

    //人脸搜索
    public static String search(String image_type, String image, String group_id, String user_id) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", image);//8fd4859cba547f2c4f89700144322502
            map.put("image_type", image_type);
            map.put("group_id_list", group_id);
//            map.put("user_id", user_id);//当需要对特定用户进行比对时，指定user_id进行比对。即人脸认证功能。
            map.put("liveness_control", "LOW");
            map.put("quality_control", "LOW");
            String param = GsonUtils.toJson(map);

            String result = HttpUtil.post(url, AuthService.getAuth(), "application/json", param);
            AppUtil.showLog(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            AppUtil.showLog("人脸搜索异常：" + e);
        }
        return null;
    }

    //人脸对比
    public static String match() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {

//            byte[] bytes1 = FileUtil.readFileByBytes("【本地文件1地址】");
//            byte[] bytes2 = FileUtil.readFileByBytes("【本地文件2地址】");
//            String image1 = Base64Util.encode(bytes1);
//            String image2 = Base64Util.encode(bytes2);

            List<Map<String, Object>> images = new ArrayList<>();

            Map<String, Object> map1 = new HashMap<>();
            map1.put("image", "http://pic.qqtn.com/up/2019-2/2019022108024192583.jpg");
//            map1.put("image", "http://f1.haiqq.com/allimg/3659695903/3671994069.jpg");//24eee3feed33a40b2b6c00abe7228674
            map1.put("image_type", "URL");
            map1.put("face_type", "LIVE");
            map1.put("quality_control", "LOW");
            map1.put("liveness_control", "LOW");

            Map<String, Object> map2 = new HashMap<>();
            map2.put("image", "http://f1.haiqq.com/allimg/2740815656/569059529.jpg");
            map2.put("image_type", "URL");
            map2.put("face_type", "LIVE");
            map2.put("quality_control", "LOW");
            map2.put("liveness_control", "LOW");

            images.add(map1);
            images.add(map2);

            String param = new Gson().toJson(images);

            String result = HttpUtil.post(url, AuthService.getAuth(), "application/json", param);
            AppUtil.showLog("人脸对比结果：" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            AppUtil.showLog("人脸对比结果异常：" + e);
        }
        return null;
    }

    public static String getList(String group_id, String user_id) {
        // 请求url
//        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/get";//用户信息查询
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers";//获取用户列表
//        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist";//获取用户人脸列表
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("group_id", group_id);
            map.put("user_id", user_id);

            String param = GsonUtils.toJson(map);

            String result = HttpUtil.post(url, AuthService.getAuth(), "application/json", param);
            AppUtil.showLog(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}